package com.ets.User_DAO;

import com.ets.common.DataDictionaryConstants;
import com.ets.common.Displayable;
import com.ets.common.StringUtils;
import com.ets.common.Validator;

/******************************************************************************* 
 The <big><b>User</b></big> class implements the <b>Displayable</b> &
 <b>Cloneable</b> interfaces to create a <b>User</b> object
 <br><br>
 @author D.Merkling - 1:33:38 PM - Aug 22, 2011
 <br><br>
 @version 1.2  
*******************************************************************************/ 
public class User implements Displayable, Cloneable, DataDictionaryConstants
  {
   private String User;
   private String Pswd;
   private String Nm_Pfx;
   private String Nm_Fst;
   private String Nm_Mid;
   private String Nm_Lst;
   private String Nm_Sfx;
   private int    Addr;
   private String Phn1;
   private String Phn2;
   private String Email;
   private String Email2;

   /******************************************************************************* 
	This constructor creates a new <b>User</b> object with default values
   *******************************************************************************/ 
   public User ()
	 {
	  this.User   = " ";
	  this.Pswd   = " ";
	  this.Nm_Pfx = " ";
	  this.Nm_Fst = " ";
	  this.Nm_Mid = " ";
	  this.Nm_Lst = " ";
	  this.Nm_Sfx = " ";
	  this.Addr   = 0;
	  this.Phn1   = " ";
	  this.Phn2   = " ";
	  this.Email  = " ";
	  this.Email2 = " ";
	 }

   /******************************************************************************* 
	This constructor creates a new <b>User</b> object with attribute values as
	defined by the provided parameters
    <br><br>
	@param User A <b><i>String</i></b> variable
	@param Pswd A <b><i>String</i></b> variable
	@param Name - prefix A <b><i>String</i></b> variable
	@param Name - first A <b><i>String</i></b> variable
	@param Name - middle A <b><i>String</i></b> variable
	@param Name - last A <b><i>String</i></b> variable
	@param Name - suffix A <b><i>String</i></b> variable
	@param Addr An <b><i>int</i></b> variable
	@param Phone #1 A <b><i>String</i></b> variable
	@param Phone #2 A <b><i>String</i></b> variable
	@param Email #1 A <b><i>String</i></b> variable
	@param Email #2 A <b><i>String</i></b> variable
   *******************************************************************************/ 
   public User (String User, String Pswd, String Nm_Pfx, String Nm_Fst, String Nm_Mid, String Nm_Lst, String Nm_Sfx, int Addr,
		                     String Phn1, String Phn2,   String Email,  String Email2)
	 {
	  this.User   = User;
	  this.Pswd   = Pswd;
	  this.Nm_Pfx = Nm_Pfx;
	  this.Nm_Fst = Nm_Fst;
	  this.Nm_Mid = Nm_Mid;
	  this.Nm_Lst = Nm_Lst;
	  this.Nm_Sfx = Nm_Sfx;
	  this.Addr   = Addr;
	  this.Phn1   = Phn1;
	  this.Phn2   = Phn2;
	  this.Email  = Email;
	  this.Email2 = Email2;
	 }

   /******************************************************************************* 
	Clones a current <b>User</b> object to create a new one
    <br><br>
	@return User - A <b>User</b> object
	@throws CloneNotSupportedException 
   *******************************************************************************/ 
   public Object clone () throws CloneNotSupportedException
	 {
	  return super.clone();
	 }

   /******************************************************************************* 
	Sets the <b><i>String</i></b> <b>User</b> attribute for the current <b>User</b>
	object
	<br><br>
	The value must be uppercase, trimmed, & left-justified
	<br><br>
	@param User A <b><i>String</i></b> variable
   *******************************************************************************/ 
   public void setUser (String usr)
	 {
	  String tmpUsr = usr.toUpperCase();
	  tmpUsr        = tmpUsr.trim();
	  this.User     = tmpUsr;
	 }

   /******************************************************************************* 
	Retrieves the <b><i>String</i></b> <b>User</b> attribute from the current
	<b>User</b> object
	<br><br>
	@return User - A <b><i>String</i></b> variable
   *******************************************************************************/ 
   public String getUser ()
	 {
	  return User;
	 }

   /******************************************************************************* 
	Sets the <b><i>String</i></b> <b>Pswd</b> attribute for the current <b>User</b>
	object
	<br><br>
	The value must be trimmed & left-justified
	<br><br>
    <big><b>NOTE:</b></big> The password must be encrypted before it is stored in
    the database. This will be accomplished in the UserDB class. The string value
	supplied in this method must be raw text - NOT ENCRYPTED. 
	<br><br>
	@param Pswd A <b><i>String</i></b> variable
   *******************************************************************************/ 
   public void setPswd (String pswd)
	 {
	  String tmpPswd = pswd.trim();
	  this.Pswd      = tmpPswd;
	 }

   /******************************************************************************* 
	Retrieves the <b><i>String</i></b> <b>Pswd</b> attribute from the current
	<b>User</b> object
	<br><br>
    <big><b>NOTE:</b></big> The password is stored in the database in an encrypted
    form. The UserDB class will decrypt it before returning it to this method. The
    password retrieved by this method will be raw text - NOT ENCRYPTED. 
	<br><br>
	@return Pswd - A <b><i>String</i></b> variable
   *******************************************************************************/ 
   public String getPswd ()
	 {
	  return Pswd;
	 }
   
   /******************************************************************************* 
	Sets the <b><i>String</i></b> <b>name prefix</b> attribute for the current
	<b>User</b> object
	<br><br>
	The value must be trimmed & left-justified
	<br><br>
	@param Name - prefix A <b><i>String</i></b> variable
   *******************************************************************************/ 
   public void setNmPfx (String nmPfx)
     {
	  String tmpNm = nmPfx.trim();
	  this.Nm_Pfx  = tmpNm;
	 }

   /******************************************************************************* 
 	Retrieves the <b><i>String</i></b> <b>prefix name</b> attribute from the
 	current <b>User</b> object
	<br><br>
	@return Name - prefix - A <b><i>String</i></b> variable
   *******************************************************************************/ 
   public String getNmPfx ()
	 {
	  return Nm_Pfx;
	 }

   /******************************************************************************* 
	Sets the <b><i>String</i></b> <b>first name</b> attribute for the current
	<b>User</b> object
	<br><br>
	The value must be trimmed & left-justified
	<br><br>
	@param Name - first A <b><i>String</i></b> variable
   *******************************************************************************/ 
   public void setNmFst (String nmFst)
	 {
	  String tmpNm = nmFst.trim();
	  this.Nm_Fst  = tmpNm;
	 }

   /******************************************************************************* 
	Retrieves the <b><i>String</i></b> <b>first name</b> attribute from the current
	<b>User</b> object
	<br><br>
	@return Name - first - A <b><i>String</i></b> variable
   *******************************************************************************/ 
   public String getNmFst ()
	 {
	  return Nm_Fst;
	 }

   /******************************************************************************* 
	Sets the <b><i>String</i></b> <b>middle name</b> attribute for the current
	<b>User</b> object
	<br><br>
	The value must be trimmed & left-justified
	<br><br>
	@param Name - middle A <b><i>String</i></b> variable
   *******************************************************************************/ 
   public void setNmMid (String nmMid)
	 {
	  String tmpNm = nmMid.trim();
	  this.Nm_Mid  = tmpNm;
	 }

   /******************************************************************************* 
	Retrieves the <b><i>String</i></b> <b>middle name</b> attribute from the
	current <b>User</b> object
	<br><br>
	@return Name - middle - A <b><i>String</i></b> variable
   *******************************************************************************/ 
   public String getNmMid ()
	 {
	  return Nm_Mid;
	 }
   
   /******************************************************************************* 
	Sets the <b><i>String</i></b> <b>last name</b> attribute for the current
	<b>User</b> object
	<br><br>
	The value must be trimmed & left-justified
	<br><br>
	@param Name - last A <b><i>String</i></b> variable
   *******************************************************************************/ 
   public void setNmLst (String nmLst)
	 {
	  String tmpNm = nmLst.trim();
	  this.Nm_Lst  = tmpNm;
	 }

   /******************************************************************************* 
	Retrieves the <b><i>String</i></b> <b>last name</b> attribute from the current
	<b>User</b> object
	<br><br>
	@return Name - last - A <b><i>String</i></b> variable
   *******************************************************************************/ 
   public String getNmLst ()
	 {
	  return Nm_Lst;
	 }
   
   /******************************************************************************* 
	Sets the <b><i>String</i></b> <b>name suffix</b> attribute for the current
	<b>User</b> object
	<br><br>
	The value must be trimmed & left-justified
	<br><br>
	@param Name - suffix A <b><i>String</i></b> variable
   *******************************************************************************/ 
   public void setNmSfx (String nmSfx)
	 {
	  String tmpNm = nmSfx.trim();
	  this.Nm_Sfx  = tmpNm;
	 }

   /******************************************************************************* 
	Retrieves the <b><i>String</i></b> <b>name suffix</b> attribute from the current
	<b>User</b> object
	<br><br>
	@return Name - suffix - A <b><i>String</i></b> variable
   *******************************************************************************/ 
   public String getNmSfx ()
	 {
	  return Nm_Sfx;
	 }

   /******************************************************************************* 
	Sets the <b><i>int</i></b> <b>Addr</b> attribute for the current
	<b>User</b> object
	<br><br>
	The value must be an unsigned, positive integer & must exist in the Address
	table
	<br><br>
	@param Addr An <b><i>int</i></b> variable
	@return Success/failure - A <b><i>boolean</i></b> variable
   *******************************************************************************/ 
   public boolean setAddr (int addr)
	 {
	  boolean isValid = Validator.isAddressValid(addr);
	  if (isValid)
	    {
		 this.Addr = addr;
		 return true;  
	    }
	  else
	    {
		 this.Addr = 0;
		 return false; 
	    }
	 }

   /******************************************************************************* 
	Retrieves the <b><i>int</i></b> <b>Addr</b> attribute from the current
	<b>User</b> object
	<br><br>
	@return Addr - An <b><i>int</i></b> variable
   *******************************************************************************/ 
   public int getAddr ()
	 {
	  return Addr;
	 }

   /******************************************************************************* 
	Sets the <b><i>String</i></b> <b>phone #1</b> attribute for the current
	<b>User</b> object
	<br><br>
	The value must be trimmed, left-justified, & formatted as a proper phone
	number - 999-999-9999
	<br><br>
	@param Phone #1 A <b><i>String</i></b> variable
	@return isValid - A <b><i>boolean</i></b> variable
   *******************************************************************************/ 
   public boolean setPhn1 (String inPhn)
	 {
	  String phn1   = inPhn.trim();
	  String phn2   = "";
      phn2 = Validator.validatePhone(phn1);
	  if (phn2.equals("**BAD**"))
	    {
		 System.out.println("Phone #1 not valid; saved as blanks");
		 return false;
	    }
	  else
	    {
		 this.Phn1 = phn2;
		 return true;
	    }
	 }

   /******************************************************************************* 
	Retrieves the <b><i>String</i></b> <b>phone #1</b> attribute from the current
	<b>User</b> object
	<br><br>
	@return Phone #1 - A <b><i>String</i></b> variable
   *******************************************************************************/ 
   public String getPhn1 ()
	 {
	  return Phn1;
	 }
  
   /******************************************************************************* 
	Sets the <b><i>String</i></b> <b>phone #2</b> attribute for the current
	<b>User</b> object
	<br><br>
	The value must be trimmed, left-justified, & formatted as a proper phone
	number - 999-999-9999
	<br><br>
	@param Phone #2 A <b><i>String</i></b> variable
	@return isValid - A <b><i>boolean</i></b> variable
   *******************************************************************************/ 
   public boolean setPhn2 (String inPhn)
	 {
	  String phn1   = inPhn.trim();
	  String phn2   = "";
      phn2 = Validator.validatePhone(phn1);
	  if (phn2.equals("**BAD**"))
	    {
		 System.out.println("Phone #2 not valid; saved as blanks");
		 return false;
	    }
	  else
	    {
		 this.Phn2 = phn2;
		 return true;
	    }
	 }

   /******************************************************************************* 
	Retrieves the <b><i>String</i></b> <b>phone #2</b> attribute from the current
	<b>User</b> object
	<br><br>
	@return Phone #2 - A <b><i>String</i></b> variable
   *******************************************************************************/ 
   public String getPhn2 ()
	 {
	  return Phn2;
	 }
   
   /******************************************************************************* 
	Sets the <b><i>String</i></b> <b>email #1</b> attribute for the current
	<b>User</b> object
	<br><br>
	The value must be trimmed, left-justified, & formatted as a proper email
	address - xxxxxxxx@yyyyyyyy.zzz
	<br><br>
	@param Email #1 A <b><i>String</i></b> variable
	@return isValid - A <b><i>boolean</i></b> variable
   *******************************************************************************/ 
   public boolean setEmail (String inEml)
	 {
	  String  eml1    = inEml.trim();
	  boolean isValid = false;
	  isValid         = Validator.validateEmail(eml1);
	  if (isValid)
	    {
		 this.Email = eml1;
		 return true;
	    }
	  else
	    {
		 System.out.println("Email #1 not valid format; saved as blanks");
		 return false;
	    }
	 }

   /******************************************************************************* 
	Retrieves the <b><i>String</i></b> <b>email #1</b> attribute from the current
	<b>User</b> object
	<br><br>
	@return Email #1 - A <b><i>String</i></b> variable
   *******************************************************************************/ 
   public String getEmail ()
	 {
	  return Email;
	 }
   
   /******************************************************************************* 
	Sets the <b><i>String</i></b> <b>email #2</b> attribute for the current
	<b>User</b> object
	<br><br>
	The value must be trimmed, left-justified, & formatted as a proper email
	address - xxxxxxxx@yyyyyyyy.zzz
	<br><br>
	@param Email #2 A <b><i>String</i></b> variable
	@return isValid - A <b><i>boolean</i></b> variable
   *******************************************************************************/ 
   public boolean setEmail2 (String inEml)
	 {
	  String  eml1    = inEml.trim();
	  boolean isValid = false;
	  isValid         = Validator.validateEmail(eml1);
	  if (isValid)
	    {
		 this.Email2 = eml1;
		 return true;
	    }
	  else
	    {
		 System.out.println("Email #2 not valid format; saved as blanks");
		 return false;
	    }
	 }

   /******************************************************************************* 
	Retrieves the <b><i>String</i></b> <b>email #2</b> attribute from the current
	<b>User</b> object
	<br><br>
	@return Email #2 - A <b><i>String</i></b> variable
   *******************************************************************************/ 
   public String getEmail2 ()
	 {
	  return Email2;
	 }
   
   /******************************************************************************* 
	Determines whether or not all of the attribute values of one <b>User</b>
	object are equal to all of the attribute values of a second <b>User</b>
	object
	<br><br>
    <big><b>NOTE:</b></big> The password column will not be included in the
    comparison logic of this method
    method
	<br><br>
	@param object A	<b>User</b> object
	@return true/false - A <b><i>boolean</i></b> variable
   *******************************************************************************/ 
   public boolean equals(Object object)
     {
      if (object instanceof User)
        {
         User user2 = (User) object;
         if (User.equals(user2.getUser())     &&
             Nm_Pfx.equals(user2.getNmPfx())  &&
             Nm_Fst.equals(user2.getNmFst())  &&
             Nm_Mid.equals(user2.getNmMid())  &&
             Nm_Lst.equals(user2.getNmLst())  &&
             Nm_Sfx.equals(user2.getNmSfx())  &&
             Addr == user2.getAddr()          &&
             Phn1.equals(user2.getPhn1())     &&
             Phn2.equals(user2.getPhn2())     &&
             Email.equals(user2.getEmail())   &&
             Email2.equals(user2.getEmail2()))
         return true;
        }
      return false;
     }

   /******************************************************************************* 
	Retrieves a one-line formatted list of the attribute values of the current
	<b>User</b> object for use in a table display
	<br><br>
    <big><b>NOTE:</b></big> The password column will not be included in the string
    provided by this method
	<br><br>
	<br><br>
	@return string - A <b><i>String</i></b> variable
   *******************************************************************************/ 
   public String toTableString ()
	 {
	  return StringUtils.padWithSpaces(this.User,   User_LEN)  + " | " +
	         StringUtils.padWithSpaces(this.Nm_Pfx, Nm_LEN)    + " | " +
	         StringUtils.padWithSpaces(this.Nm_Fst, Nm_LEN)    + " | " +
	         StringUtils.padWithSpaces(this.Nm_Mid, Nm_LEN)    + " | " +
	         StringUtils.padWithSpaces(this.Nm_Lst, Nm_LEN)    + " | " +
	         StringUtils.padWithSpaces(this.Nm_Sfx, Nm_LEN)    + " | " +
	         this.Addr                                         + " | " +
	         StringUtils.padWithSpaces(this.Phn1,   Phn_LEN)   + " | " +
	         StringUtils.padWithSpaces(this.Phn2,   Phn_LEN)   + " | " +
	         StringUtils.padWithSpaces(this.Email,  Email_LEN) + " | " +
	         StringUtils.padWithSpaces(this.Email2, Email_LEN);
	 }
   
   /******************************************************************************* 
	Retrieves a formatted list of the attribute values of the current
	<b>User</b> object
	<br><br>
    <big><b>NOTE:</b></big> The password column will not be included in the string
    provided by this method
	<br><br>
	@return string - A <b><i>String</i></b> variable
   *******************************************************************************/ 
   public String toString()
	 {
	  return "User       : " + this.User        + "\n" +
	         "Name prefix: " + this.Nm_Pfx      + "\n" +
		     "First name : " + this.Nm_Fst      + "\n" +
		     "Middle name: " + this.Nm_Mid      + "\n" +
		     "Last name  : " + this.Nm_Lst      + "\n" +
		     "Name suffix: " + this.Nm_Sfx      + "\n" +
		     "Address ID : " + this.Addr        + "\n" +
		     "Phone #1   : " + this.getPhn1()   + "\n" +
		     "Phone #2   : " + this.getPhn2()   + "\n" +
	         "Email #1   : " + this.getEmail()  + "\n" +
             "Email #2   : " + this.getEmail2() + "\n";
	 }
   
   /******************************************************************************* 
	Retrieves a formatted displayable list of the attribute values of the current
	<b>User</b> object
	<br><br>
	@return string - A <b><i>String</i></b> variable
   *******************************************************************************/ 
   public String getDisplayText()
    {
	  return toString();
    }
  }